package es.cmesa.agui;

import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import es.cmesa.dcontroladores.ControladorCliente;
import es.cmesa.fexcecoes.SenhaIncorretaException;

public class login extends JInternalFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtDigitelogin;
	private JButton btnLogar;
	
	private static boolean logado;
	private JPasswordField pwdSenha;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					login frame = new login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public login() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 210, 166);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		setClosable(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		logado = false;
		
		JLabel lblLogin = new JLabel("Login");
		lblLogin.setBounds(10, 14, 46, 14);
		contentPane.add(lblLogin);
		
		JLabel lblSenha = new JLabel("Senha");
		lblSenha.setBounds(10, 58, 46, 14);
		contentPane.add(lblSenha);
		
		txtDigitelogin = new JTextField();
		txtDigitelogin.setText("");
		txtDigitelogin.setBounds(66, 11, 120, 20);
		contentPane.add(txtDigitelogin);
		txtDigitelogin.setColumns(10);
		
		btnLogar = new JButton("logar");
		btnLogar.setBounds(97, 96, 89, 23);
		contentPane.add(btnLogar);
		
		pwdSenha = new JPasswordField();
		pwdSenha.setText("");
		pwdSenha.setBounds(71, 55, 115, 20);
		contentPane.add(pwdSenha);
		
		ButtonHandler handler = new ButtonHandler();
		btnLogar.addActionListener(handler);
	}
	
	public JTextField getTxtDigitelogin() {
		return txtDigitelogin;
	}

	public void setTxtDigitelogin(JTextField txtDigitelogin) {
		this.txtDigitelogin = txtDigitelogin;
	}

	public JPasswordField getPwdSenha() {
		return pwdSenha;
	}

	public void setPwdSenha(JPasswordField pwdSenha) {
		this.pwdSenha = pwdSenha;
	}

	public static boolean getLogado() {
		return logado;
	}

	public void setLogado(boolean logado) {
		this.logado = logado;
	}

	private class ButtonHandler implements ActionListener{

		@SuppressWarnings("deprecation")
		@Override
		public void actionPerformed(ActionEvent event) {
			if(event.getSource() == btnLogar){
				ControladorCliente controlador = new ControladorCliente();
					try {
						if(controlador.efetuarLogin(txtDigitelogin.getText(), pwdSenha.getText())){
							JOptionPane.showMessageDialog(null,"Seja bem vindo " + txtDigitelogin.getText());
							logado = true;
							doDefaultCloseAction();
						}else{
							JOptionPane.showMessageDialog(null,"Usuario ou Senha invalido(s)");
							txtDigitelogin.setText("");
							pwdSenha.setText("");
						   	txtDigitelogin.grabFocus();
						}
					} catch (HeadlessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SenhaIncorretaException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
	}
	
}
